package com.jason.www.jdk8;


import com.jason.www.jdk8.code.Status;
import com.jason.www.jdk8.pojo.Person;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class D_StreamAPI_3 {
    List<Person> persons = Arrays.asList(
            new Person(2, "钱四", 24, Status.YOUNG),
            new Person(1, "张三", 23,Status.YOUNG),
            new Person(2, "李四", 24,Status.YOUNG),
            new Person(3, "王五", 65,Status.OLD),
            new Person(4, "赵六", 26,Status.MIDDLE),
            new Person(4, "赵六", 56,Status.OLD),
            new Person(5, "陈七", 27,Status.MIDDLE)
    );

    //查找与匹配
    @Test
    public void test1(){
        boolean b = persons.stream()
                .allMatch((e) -> e.getStatus().equals(Status.YOUNG));
        System.out.println(b);

        boolean b2 = persons.stream()
                .anyMatch((e) -> e.getStatus().equals(Status.YOUNG));
        System.out.println(b2);

        boolean b3 = persons.stream()
                .noneMatch((e) -> e.getStatus().equals(Status.MIDDLE));
        System.out.println(b3);

        Optional<Person> op = persons.stream()
                .sorted((e1,e2) -> Integer.compare(e1.getAge(), e2.getAge()))
                .findFirst();
        System.out.println(op.get());

        Optional<Person> optional = persons.stream()
                .filter((e) -> e.getStatus().equals(Status.OLD))
                .findAny();
        System.out.println(optional.get());
    }

    //最大，最小
    @Test
    public void test2(){
        long count = persons.stream()
                .count();
        System.out.println(count);

        Optional<Person> optional = persons.stream()
                .max((e1,e2) -> Integer.compare(e1.getId(), e2.getId()));
        System.out.println(optional.get());

        Optional<Integer> op = persons.stream()
                .map(Person :: getAge)
                .min(Integer :: compare);
        System.out.println(op.get());
    }

    //归约
    @Test
    public void test3(){
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8);

        Integer sum = list.stream()
                .reduce(0, (x,y) -> x + y);
        System.out.println(sum);
        System.out.println("------------");

        Optional<Integer> optional = persons.stream()
                .map(Person :: getAge)
                .reduce(Integer :: sum);
        System.out.println(optional.get());

        /*Iterator<Person> iterator = persons.iterator();
        Integer result = 0;
        while (iterator.hasNext())  result += iterator.next().getAge();
        System.out.println(result);*/

    }

    //收集
    @Test
    public void test4(){
        List<String> list = persons.stream()
                .map(Person :: getName)
                .collect(Collectors.toList());
        list.forEach(System.out :: println);
        System.out.println("------------");

        Set<String> set = persons.stream()
                .map(Person :: getName)
                .collect(Collectors.toSet());
        set.forEach(System.out :: println);
        System.out.println("------------");

        HashSet<String> hashSet = persons.stream()
                .map(Person :: getName)
                .collect(Collectors.toCollection(HashSet :: new));
        hashSet.forEach(System.out :: println);
    }

    @Test
    public void test5(){
        Long count = persons.stream()
                .collect(Collectors.counting());
        System.out.println("总人数="+count);
        System.out.println("----------------");

        //平均值
        Double avg = persons.stream()
                .collect(Collectors.averagingInt(Person :: getAge));
        System.out.println("平均年龄="+avg);
        System.out.println("---------------");

        //总和
        Integer sum = persons.stream()
                .collect(Collectors.summingInt(Person :: getAge));
        System.out.println("年龄总和="+sum);
        System.out.println("----------------");

        //最大值
        Optional<Person> max = persons.stream()
                .collect(Collectors.maxBy((e1,e2) -> Integer.compare(e1.getAge(), e2.getAge())));
        System.out.println("最大年龄是"+max.get());
        System.out.println("----------------");

        //最小值
        Optional<Person> min = persons.stream()
                .collect(Collectors.minBy((e1,e2) -> Integer.compare(e1.getAge(), e2.getAge())));
        System.out.println("最小年龄是"+min.get());
    }

    //分组
    @Test
    public void test6(){
        Map<Status, List<Person>> map = persons.stream()
                .collect(Collectors.groupingBy(Person :: getStatus));//根据年龄层分组
        System.out.println(map);
    }

    //多级分组
    @Test
    public void test7(){
        Map<Status, Map<String, List<Person>>> map = persons.stream()
                .collect(Collectors.groupingBy(Person :: getStatus ,Collectors.groupingBy((e) -> {
                    if (e.getId()%2 == 1) {
                        return "单号";
                    } else {
                        return "双号";
                    }
                })));
        System.out.println(map);
    }

    //分区
    @Test
    public void test8(){
        Map<Boolean, List<Person>> map = persons.stream()
                .collect(Collectors.partitioningBy((e) -> e.getAge() > 30));
        System.out.println(map);
    }

    //IntSummaryStatistics
    @Test
    public void test9(){
        IntSummaryStatistics iss = persons.stream()
                .collect(Collectors.summarizingInt(Person :: getAge));
        System.out.println(iss.getSum());
        System.out.println(iss.getAverage());
        System.out.println(iss.getMax());
    }

    @Test
    public void test10(){
        String str = persons.stream()
                .map(Person :: getName)
                .collect(Collectors.joining(",","人员名单：","等"));
        System.out.println(str);
    }
}
